package com.tencent.product.service.impl;

import com.tencent.basic.service.IBaiduAiAuditService;
import com.tencent.basic.service.serviceimpl.BaseServiceImpl;
import com.tencent.basic.util.AjaxResult;
import com.tencent.basic.util.LoginContext;
import com.tencent.org.domain.Emp;
import com.tencent.org.mapper.EmpMapper;
import com.tencent.pet.domain.Pet;
import com.tencent.pet.domain.PetDetail;
import com.tencent.pet.domain.PetOnlineAuditLog;
import com.tencent.product.domain.Product;
import com.tencent.product.domain.ProductDetail;
import com.tencent.product.domain.ProductOnlineAuditLog;
import com.tencent.product.mapper.ProductDetailMapper;
import com.tencent.product.mapper.ProductMapper;
import com.tencent.product.mapper.ProductOnlineAuditLogMapper;
import com.tencent.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Override
    @Transactional
    public void add(Product product) {
        super.add(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null){
            productDetail.setProductId(product.getId());
            productDetailMapper.add(productDetail);
        }
    }

    @Override
    @Transactional
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null){
            productDetail.setProductId(product.getId());
            productDetailMapper.add(productDetail);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        super.del(id);
        productDetailMapper.deleteByProductId(id);
    }

    @Override
    @Transactional
    public void patchDel(List<Long> ids) {
        super.patchDel(ids);
        productDetailMapper.patchDeleteByProductId(ids);
    }

    @Override
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        for (Long id : ids) {
            Product product = productMapper.findById(id);
            Boolean boo1 = baiduAiAuditService.textAudit(product.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(product.getResources());

            if(boo1 && boo2){
                productMapper.onsale(new Date(), id);
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                // 获取登录信息
                Long logininfo = LoginContext.getLogininfo(request).getId();
                Emp emp = empMapper.findByLogininfoId(logininfo);
                log.setProductId(id);
                log.setState(1);
                log.setAuditId(emp.getId());
                log.setNote("审核成功");
                productOnlineAuditLogMapper.add(log);
                return new AjaxResult();
            }else {
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                // 获取登录信息
                Long logininfo = LoginContext.getLogininfo(request).getId();
                Emp emp = empMapper.findByLogininfoId(logininfo);
                log.setProductId(id);
                log.setState(0);
                log.setAuditId(emp.getId());
                log.setNote("审核失败");
                productOnlineAuditLogMapper.add(log);
                return AjaxResult.me().setSuccess(false).setMsg("审核失败");
            }

        }
        return new AjaxResult();
    }

    @Override
    public void offsale(List<Long> ids, HttpServletRequest request) {
        // 1.改下价时间为当前系统时间，该状态为0
        productMapper.offsale(new Date(), ids);
    }
}
